{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "180e2db1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 记录人：曾烨"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7438bc54",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "\n",
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "\n",
    "    timer = time.perf_counter\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "    if sys.platform == \"win32\":\n",
    "        timer = time.clock\n",
    "    else:\n",
    "        timer = time.time\n",
    "\n",
    "def fetch_token(API_KEY,SECRET_KEY):\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request('http://aip.baidubce.com/oauth/2.0/token', post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "    result = json.loads(result_str)\n",
    "    if ('access_token' in result.keys() ):\n",
    "        return result['access_token']\n",
    "\n",
    "        \n",
    "def asr(token,AUDIO_FILE='1.wav',CUID='123456PYTHON',DEV_PID='1537',RATE = 16000):\n",
    "    speech_data = []\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + AUDIO_FILE[-3:] + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = 'http://vop.baidu.com/server_api' + \"?\" + params_query\n",
    "    req = Request('http://vop.baidu.com/server_api' + \"?\" + params_query, speech_data, headers)\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "        print(\"Request time cost %f\" % (timer() - begin))\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')\n",
    "#     print(result_str)\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)\n",
    "    return result_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5fd5a43c",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = '8w02VS2m62ogGlakwdv7Pcz2'\n",
    "SECRET_KEY = 'qDWWuSWtz8u4qyekXh9CG6xbrrtLQGAo'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ebccf87f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.ef8bfc0526df68483be93e27c23623bf.2592000.1655292364.282335-26192467'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "s_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "de0b84e6",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-8-aef7c7fe7a9f>, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-8-aef7c7fe7a9f>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m    with open(\"1.wav\",\"wb\") as f\u001b[0m\n\u001b[1;37m                                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 保存到wav文件中\n",
    "with open(\"1.wav\",\"wb\") as f\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cc1c329",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr(token = s_token,AUDIO = '1.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bec456fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 图灵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "50a315c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_KEY = ''\n",
    "TL_ur = ''\n",
    "\n",
    "payload={\n",
    "    \"reType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"你好\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\" : {}\n",
    "    \"userId\": \"000001\"\n",
    "}\n",
    "\n",
    "payload_json = json.dumps(payload)\n",
    "\n",
    "print(payload)\n",
    "res = requests.post(TL_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e0f7c47",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78322b46",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9220c11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e7ac264",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c404aa09",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07f19a14",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37975559",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8695a268",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d76d75a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b44f6c3b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b817d82",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
